import { HttpSeriveProvider } from './../http-serive/http-serive';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';

/*
  Generated class for the MySeriveProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class MySeriveProvider {
  public headers = new HttpHeaders();

  apiUpdataImg = 'user/update';
  apiMeInfo = 'user/me/info?param='; // 获得个人信息接口
  apiExit = 'user/exit'; // 退出信息接口
  apiIsLogin = 'check/token'; // 检验登录状态
  apiCheckAdmin = 'user/check/admin';
  apiAllend = 'evaluate/allend';
  apimember= 'evaluate/allend/user';
  apiSorce = 'evaluate/allend/user/sorce'
  token;

  // 时间戳
  date = '?' + new Date();

  apiCheckPassword = 'user/check/password'; // 修改密码 检验旧密码

  constructor(public http: HttpClient, public httpService: HttpSeriveProvider) {
    this.token = localStorage.getItem('token');

    this.headers = this.headers.set('Content-Type', 'text/plain');
    this.headers = this.headers.set('Content-Type', 'application/json');
    if (this.token) {
      this.headers = this.headers.set('token', this.token);
    } else if (this.token === null || this.token === undefined) {
      this.headers = this.headers.set('token', '');
    } else {
      this.headers = this.headers.set('token', null);
    }
  }

  // 获取token
  getToken(token) {
    this.token = token;
    this.headers = this.headers.set('token', this.token);
  }

  deleteToken() {
    localStorage.removeItem('token');
    this.headers = this.headers.delete('token');
  }

  // 我的信息
  MeInfo() {
    return this.httpService.post(this.httpService.api + this.apiMeInfo, 0, {
      headers: this.headers
    });
  }

  // 退出
  logOff() {
    return this.httpService.post(this.httpService.api + this.apiExit, null, {
      headers: this.headers
    });
  }

  // 检验登录状态
  checkIsLogin() {
    return this.httpService.post(this.httpService.api + this.apiIsLogin, 0, {
      headers: this.headers
    });
  }

  // 升级信息接口
  updataImg(body) {
    return this.httpService.post(
      this.httpService.api + this.apiUpdataImg,
      body,
      { headers: this.headers }
    );
  }

  // 检验旧密码
  checkOldPassword(password) {
    return this.httpService.post(
      this.httpService.api + this.apiCheckPassword,
      password,
      {
        headers: this.headers
      }
    );
  }

  checkAdmin() {
    return this.httpService.post(
      this.httpService.api + this.apiCheckAdmin + this.date,
      0,
      {
        headers: this.headers
      }
    );
  }

  getAllend() {
    return this.httpService.post(
      this.httpService.api + this.apiAllend + this.date,
      0,
      {
        headers: this.headers
      }
    );
  }

  getMember(evaluating) {
    return this.httpService.post(
      this.httpService.api + this.apimember,
      evaluating,
      {
        headers: this.headers
      }
    );
  }

  getSorce(evaluating) {
    return this.httpService.post(
      this.httpService.api + this.apiSorce,
      evaluating,
      {
        headers: this.headers
      }
    );
  }
}
